package cn.gzjp.modules.woms.web.front;




import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import cn.gzjp.common.constant.Constants;
import cn.gzjp.modules.woms.entity.EventProcess;
import cn.gzjp.modules.woms.service.EventProcessService;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;

import cn.gzjp.common.web.BaseController;
import cn.gzjp.modules.woms.entity.Event;
import cn.gzjp.modules.woms.entity.Worker;
import cn.gzjp.modules.woms.service.EventService;
import cn.gzjp.modules.woms.service.WorkerService;


/**
 * 企业号--故障查询应用(供领导看)
 */
@Controller
@RequestMapping(value = "${frontPath}/pm")
@SessionAttributes(value = { "corpId", "agentId", "userId" })
public class PmanagerController extends BaseController {

	@Autowired
	private EventService eventService;

	/**
	 * 未完成列表
	 *
	 * @param userId
	 * @param model
	 * @return
	 */
	@RequestMapping("unfinish")
	public String unfinish(@ModelAttribute("userId") String userId, Model model) {
		Event event = new Event();
		event.setPmUnfinish("y");//未完成（status为 1234）
		List<Event> eventList = eventService.findList(event);
		Collections.sort(eventList, new Comparator<Event>(){
			public int compare(Event o1, Event o2) {
				if(o1.getLeftMinutes() > o2.getLeftMinutes()){
					return 1;
				}
				if(o1.getLeftMinutes() == o2.getLeftMinutes()){
					return 0;
				}
				return -1;
			}
		});
		model.addAttribute("eventList", eventList);
		return "modules/woms/front/pm/unfinishList";
	}


	/**
	 * 已完成
	 *
	 * @param userId
	 * @param model
	 * @return
	 */
	@RequestMapping("finished")
	public String finished(@ModelAttribute("userId") String userId, Model model) {
		Event event = new Event();
		event.setStatus(Constants.EventStatus.achieved);
		List<Event> eventList = eventService.findList(event);
		model.addAttribute("eventList", eventList);
		Collections.sort(eventList, new Comparator<Event>(){
			public int compare(Event o1, Event o2) {
				if(o1.getLeftMinutes() > o2.getLeftMinutes()){
					return 1;
				}
				if(o1.getLeftMinutes() == o2.getLeftMinutes()){
					return 0;
				}
				return -1;
			}
		});
		return "modules/woms/front/pm/finishedList";
	}

	/**
	 * 详细展示
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "event", method = RequestMethod.GET)
	public String event(String id, Model model) {
		Event event = eventService.get(id);
		model.addAttribute("event", event);
		return "modules/woms/front/pm/event";
	}

}